﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace HosPlat.API
{
    public class ToolUtil
    {
        /// <summary>
        /// 读取设备信息
        /// </summary>
        /// <param name="layaddress"></param>
        public static string GetEquInfo(string ip, string port, string equad)
        {
            equad = CRC.IntToHex(Int32.Parse(equad));
            string input = equad + "03" + "0001" + "0031";
            string crcCode = CRC.ToModbusCRC16(input);
            input = input + crcCode;

            string rec = SocketUtil.Send(ip, Int32.Parse(port), input);
            
            return rec;
        }


        //设置设备安装位置
        public static string SetSetupADD(string ip, string port, string equad,string SetupAD)
        {
            equad = CRC.IntToHex(Int32.Parse(equad));
            string input = equad + "10" + "0022" + "0010" + "20";
            string setupad = CRC.GetHexFromChs(SetupAD).PadRight(64, '0');

            input += setupad;

            string crcCode = CRC.ToModbusCRC16(input);
            input = input + crcCode;

            string rec = SocketUtil.Send(ip, Int32.Parse(port), input);

            return rec;
        }

        /// <summary>
        /// 修改设备名称
        /// </summary>
        /// <param name="name"></param>
        public static string SetEquName(string ip, string port, string equad, string equname)
        {
            equad = CRC.IntToHex(Int32.Parse(equad));

            string input = equad + "10" + "0012" + "0010" + "20";
            string setupad = CRC.GetHexFromChs(equname).PadRight(64, '0');

            input += setupad;

            string crcCode = CRC.ToModbusCRC16(input);
            input = input + crcCode;

            string rec = SocketUtil.Send(ip, Int32.Parse(port), input);

            return rec;


        }


        /// <summary>
        /// 设置设备时间
        /// </summary>
        /// <param name="layaddress"></param>
        public static string SetEquTime(string ip, string port, string equad)
        {

            equad = CRC.IntToHex(Int32.Parse(equad));

            //string hrex = CRC.GetHexFromChs(time);
            DateTime now = DateTime.Now;

            string year = CRC.IntToHex(now.Year - 2000);
            string month = CRC.IntToHex(now.Month);

            string day = CRC.IntToHex(now.Day);
            string hour = CRC.IntToHex(now.Hour);

            string minute = CRC.IntToHex(now.Minute);
            string second = CRC.IntToHex(now.Second);

            string time = year + month + day + hour + minute + second;

            string input = equad + "10" + "000A" + "0003" + "06" + time;
            string crcCode = CRC.ToModbusCRC16(input);
            input = input + crcCode;

            string rec = SocketUtil.Send(ip, Int32.Parse(port), input);

            return rec;

        }

        /// <summary>
        /// 读取通道信息
        /// </summary>
        /// <param name="equad"></param>
        /// <param name="pathad"></param>
        public static string GetPathInfo(string ip, string port, string equad, int i)
        {
            equad = CRC.IntToHex(Int32.Parse(equad));
            //string rec = "";
            ////通道数据分两批来读取

            //int sg = (qty / 4) + (qty % 4 == 0 ? 0 : 1);

            //int start = 101;
            //for (int i = 0; i < sg; i++)
            //{
            //    string input = equad + "03" + CRC.IntToHex(start + i * 80) + "0050";
            //    string crcCode = CRC.ToModbusCRC16(input);
            //    input = input + crcCode;

            //    rec = SocketUtil.Send(ip, Int32.Parse(port), input);
            //}

            //return rec;
            int start = 101;
            string input = equad + "03" + CRC.IntToHex(start + i * 20).PadLeft(4, '0') + "0014";
            string crcCode = CRC.ToModbusCRC16(input);
            input = input + crcCode;

            string rec = SocketUtil.Send(ip, Int32.Parse(port), input);

            return rec;
        }

        /// <summary>
        /// 设置通道名称
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="equad"></param>
        /// <param name="name">最多8个字</param>
        public static void SetPathName(string ip, string port, string equad, string name,int index)
        {
            equad = CRC.IntToHex(Int32.Parse(equad));
            string hrexName = CRC.GetHexFromChs(name);
            string hrexlength = CRC.IntToHex(hrexName.Length / 2);

            int start = 108;

            string input = equad + "10" + CRC.IntToHex(start + (index - 1) * 20).PadLeft(4, '0') + "0008" + hrexlength + hrexName;
            string crcCode = CRC.ToModbusCRC16(input);
            input = input + crcCode;

            string rec = SocketUtil.Send(ip, Int32.Parse(port), input);

        }


        /// <summary>
        /// 读取通道数据
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        /// <param name="equad"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string GetPathData(string ip, string port, string equad, int i)
        {
            equad = CRC.IntToHex(Int32.Parse(equad));
            int start = 401;
            string input = equad + "03" + CRC.IntToHex(start + i * 4).PadLeft(4, '0') + "0004";
            string crcCode = CRC.ToModbusCRC16(input);
            input = input + crcCode;

            string rec = SocketUtil.Send(ip, Int32.Parse(port), input);

            return rec;

            
        }

    }


}